package com.smartadmin.module.business.people;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.smartadmin.module.system.department.domain.dto.DepartmentVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class PeopleService {

    @Resource
    private PeopleDao peopleDao;

    public PageInfo<People> selectPeopleList(People people) {
        PageHelper.startPage(people.getPageNum(), people.getPageSize());
        List<People> peopleList = peopleDao.selectPeopleList(people);
        PageInfo<People> pageInfo = new PageInfo<>(peopleList);
        return pageInfo;
    }


    public List<DeptCategory> queryDepartmentById() {
        // 先查询出全部类目
        List<DeptCategory> allDeptList = peopleDao.queryDepartmentById();

//        Map<Long, Map<Long, String>> map = deptList.stream().collect(Collectors.groupingBy(DepartmentVO::getParentId,
//                Collectors.toMap(DepartmentVO::getParentId, DepartmentVO::getName)));

        // 先查询出全部类目     List<Category> categories = categoryMapper.selectCategoryAll();
        // 获取一级菜单 , 0 代表一级目录
//        List<DeptCategoryVo> rootCategory = deptList.stream()
//                .filter(e -> "0".equals(e.getParentId()))
//                .map(this :: categoryTOCategoryVo)
//                .sorted(Comparator.comparing(DeptCategoryVo :: getId).reversed())
//                .collect(Collectors.toList());
        // 查找子节点        findSubCategory(deptList, rootCategory);

        // 获取一级菜单 , 0 代表一级目录
        List<DeptCategory> rootCategory = allDeptList.stream().filter(a -> "0".equals(a.getParentId())).
                collect(Collectors.toList());

        // 为根菜单设置子菜单，getClildren是递归调用的
        for (DeptCategory dept : rootCategory) {
            /* 获取根节点下的所有子节点 使用 getChildren 方法 */
            List<DeptCategory> childList = getChildren(dept.getId(), allDeptList);
            dept.setChildren(childList);//给根节点设置子节点
        }

        return rootCategory;
    }

    /**
     * 获取子节点
     *
     * @param id          父节点id
     * @param allDeptList 所有菜单列表
     * @return 每个根节点下，所有子菜单列表
     */
    public List<DeptCategory> getChildren(String id, List<DeptCategory> allDeptList) {
        //子菜单
        List<DeptCategory> childList = new ArrayList<DeptCategory>();
        for (DeptCategory dept : allDeptList) {
            // 遍历所有节点，将所有菜单的父id与传过来的根节点的id比较
            // 相等说明：为该根节点的子节点。
            if (dept.getParentId().equals(id)) {
                childList.add(dept);
            }
        }
        //递归
        for (DeptCategory devo : childList) {
            devo.setChildren(getChildren(devo.getId(), allDeptList));
        }
//        Collections.sort(childList, order()); //排序
        // 如果节点下没有子节点，返回一个空List（递归退出）
        if (childList.size() == 0) {
            return new ArrayList<DeptCategory>();
        }
        return childList;
    }

    /**
     * 新增用户
     * @param people
     */
    public void insertPeople(People people) {

        peopleDao.insertPeople(people);
    }

    /**
     * 查询用户详情
     * @param people
     * @return
     */
    public People queryPeopleDetails(People people) {


        return peopleDao.queryPeopleDetails(people);
    }

    /**
     * 修改用户
     * @param people
     * @return
     */
    public People updatePeople(People people) {

        return peopleDao.updatePeople(people);
    }
    /**
     * 逻辑删除用户
     * @param people
     */
    public void deletePeople(People people) {
        peopleDao.deletePeople(people);
    }
    /*
     * 排序,根据order排序
     */
    public Comparator<DeptCategory> order() {
        Comparator<DeptCategory> comparator = new Comparator<DeptCategory>() {
            @Override
            public int compare(DeptCategory o1, DeptCategory o2) {
                if (o1.getOrder() != o2.getOrder()) {
                    return o1.getOrder() - o2.getOrder();
                }
                return 0;
            }
        };
        return comparator;
    }


    /**
     * 根据一级递归调用子级
     *
     * @param categoryList
     * @param rootCategory
     * @return
     */
    private void findSubCategory(List<DeptCategory> categoryList, List<DeptCategoryVo> rootCategory) {
        // 遍历一级
        for (DeptCategoryVo categoryVo : rootCategory) {
            List<DeptCategoryVo> childrenList = new ArrayList<>();
            // 查找子级
            for (DeptCategory category : categoryList) {
                // 判断当前目录是否是子父级关系
                if (categoryVo.getId().equals(category.getParentId())) {
                    childrenList.add(categoryTOCategoryVo(category));
                }
                // 递归调用，不管有几级菜单，都能够适用
                findSubCategory(categoryList, childrenList);
                // 类目显示排序，reversed 表示数字越大靠前
                childrenList.sort(Comparator.comparing(DeptCategoryVo::getId).reversed());
            }
            // 最后把查到的子级保存到一级目录中
            categoryVo.setChildren(childrenList);
        }
    }


    /**
     * 对象转换 将数据库实体类对象转还给前端实体
     *
     * @param deptCategory
     * @return
     */
    private DeptCategoryVo categoryTOCategoryVo(DeptCategory deptCategory) {
        DeptCategoryVo deptCategoryVo = new DeptCategoryVo();
        BeanUtils.copyProperties(deptCategory, deptCategoryVo);
        return deptCategoryVo;
    }

}
